package com.paic.mhis.hcpms.productClear.biz.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.productClear.constants.ProductClearConstants;
import com.paic.mhis.hcpms.productClear.dao.ProductClearDAO;
import com.paic.mhis.hcpms.productClear.dto.ProductClearDTO;
import com.paic.mhis.hcpms.productClear.dto.ProductClearParameterDTO;
import com.paic.mock.util.StringUtils;

@Component("productClearServiceImpl")
public class ProductClearServiceImpl implements IProductClearService {
	@Autowired
	private ProductClearDAO productClearDAO;

	@Override
	public  Map<String,Object> loadProductClearData(ProductClearParameterDTO vo,
			UserDetails user) {
		
		int page = vo.getPage();
		int pageSize = vo.getPageSize();

		int start = (page - 1) * pageSize;
		int end = start + pageSize;
		
		
		
	    Map<String,Object> parameterMap=new HashMap<String,Object>();
	       parameterMap.put("orgCode", vo.getOrgCode());
	       parameterMap.put("caseCode", vo.getCaseCode());
	       parameterMap.put("caseState", vo.getCaseState());
	       parameterMap.put("clearTimeStart", vo.getClearTimeStart());
	       
	       if(StringUtils.hasText(vo.getClearTimeEnd())){
	    	   String str=this.processAddOneDate(vo.getClearTimeEnd());
	    	   parameterMap.put("clearTimeEnd", str);
	       }
	       
	      
	       
	       parameterMap.put("approveTimeStart", vo.getApproveTimeStart());
	       
	       if(StringUtils.hasText(vo.getApproveTimeEnd())){
	    	   String str=this.processAddOneDate(vo.getApproveTimeEnd());
	    	   parameterMap.put("approveTimeEnd",str);
	       }
	       parameterMap.put("startNum",start);
	       parameterMap.put("endNum",end);
	     
	       String caseState=vo.getCaseState();
			if(StringUtils.hasText(caseState)){
				if(ProductClearConstants.status_001.equals(caseState)){
					 parameterMap.put("caseState","'"+ProductClearConstants.status_7+"','"+ProductClearConstants.status_N2014009+"'");
				}else{
					parameterMap.put("caseState","'"+caseState+"'");
				}
				
			}
	       
	    List<ProductClearDTO> list=this.productClearDAO.queryProductClearDTOList(parameterMap);
	    long count=this.productClearDAO.queryProductClearDTOCount(parameterMap);
	    
	    
	    
	    Map<String,Object> returnMap=new HashMap<String,Object>();  
	    
	    returnMap.put("list", list);
	    returnMap.put("count", count);
	    
	    return returnMap;
	   

	}
	
	/**
	 * currentDate 在此日期上增加一天
	 * @param currentDate
	 * @return
	 */
	private String processAddOneDate(String currentDate){
		 String returnStr="";
		
		if(StringUtils.hasText(currentDate)){
			SimpleDateFormat formate=new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date=formate.parse(currentDate);
				Calendar calendar=Calendar.getInstance();
				calendar.setTime(date);
				calendar.add(Calendar.DAY_OF_MONTH,1);
				
				Date addDate=calendar.getTime();
				
				returnStr=formate.format(addDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
		}
		
		return returnStr;
	}

	@Transactional
	@Override
	public void processProductClearData(ProductClearDTO bean, UserDetails user) {
		
		String productType=bean.getProductType();
		if(StringUtils.hasText(productType)){
			if(ProductClearConstants.product_type_01.equals(productType)){ //预警
				
				bean.setStatus(ProductClearConstants.status_11);
				bean.setCreateUser(user.getUsername());
				bean.setUpdateUser(user.getUsername());
				
				this.productClearDAO.insertProductClearBean(bean);
				this.productClearDAO.updateWaringInfo(ProductClearConstants.status_11,bean.getCode());
				
				
			}else if(ProductClearConstants.product_type_02.equals(productType)){ //稽核
				
				bean.setStatus(ProductClearConstants.status_11);
				bean.setCreateUser(user.getUsername());
				bean.setUpdateUser(user.getUsername());
				
				this.productClearDAO.insertProductClearBean(bean);
				this.productClearDAO.updateAuditCaseInfo(ProductClearConstants.status_11,bean.getCode());
			
			}
		}
		
	}

	@Transactional
	@Override
	public void batchProcessProductClearData(List<ProductClearDTO> beanList,
			UserDetails user) {
		if(beanList!=null && beanList.size()>0){
			for(ProductClearDTO bean : beanList){
				
				String status=bean.getStatus();
				if(StringUtils.hasText(status)){
					if(ProductClearConstants.status_N2014009.equals(status) || ProductClearConstants.status_7.equals(status)){
						this.processProductClearData(bean, user);
					}
				}
				
			}
		}
		
	}

	@Override
	public List<ProductClearDTO> getProductClearDTOByParameter(
			Map<String, Object> map) {
		
		List<ProductClearDTO> list=this.productClearDAO.queryProductClearDTOList(map);
		return list;
	}

	@Override
	public long getCountProductClearDTOByParamter(Map<String, Object> map) {
		
		 return this.productClearDAO.queryProductClearDTOCount(map);
	}

}
